import React from 'react'
import { message, Modal } from 'antd'
import { ExclamationCircleOutlined } from '@ant-design/icons'
import { awaitWrap } from '@js/tool'
import { getDetailRequest, updateModalStatus, getEmsDetailRequest, sendElectronicRequest } from './ajax'
const { confirm } = Modal
// 获取详情，并显示相应的操作弹框
export async function getDetail (id, dispatch, refreshTable = () => {}, modelKey) {
  const hide = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getDetailRequest(id))
  hide()
  if (e) {
    return message.error(e.message)
  }
  dispatch({ type: 'currentService', data: d })
  dispatch({ type: modelKey, data: true })
  dispatch({ type: 'refreshCurrentTableFn', data: refreshTable })
  dispatch({ type: 'refreshCurrentServiceFn', data: () => refreshCurrentServiceFn(id, dispatch) })
}

export async function refreshCurrentServiceFn (id, dispatch) {
  const hide = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getDetailRequest(id))
  hide()
  if (e) {
    return message.error(e.message)
  }
  dispatch({ type: 'currentService', data: d })
}

// 更新送达状态
export async function updateDetailStatus (id, status, refreshTable = () => {}) {
  const hide = message.loading('更新中', 120)
  const [e] = await awaitWrap(updateModalStatus(id, status))
  hide()
  if (e) {
    return message.error(e.message)
  }
  refreshTable()
}

// 作废
export function delService (id, refreshTable = () => {}) {
  confirm({
    icon: <ExclamationCircleOutlined />,
    content: '您确定要作废该送达吗？',
    onOk: () => {
      updateDetailStatus(id, '作废', refreshTable)
    }
  })
}

// 打印邮单
export async function print (common, id, caseid, theme, refreshTable, validatable = false) {
  const hide = message.loading('加载中', 120)
  const [err, detail] = await awaitWrap(getDetailRequest(id))
  if (err) {
    hide()
    return message.error(err.message)
  }
  if (validatable && theme.type !== '仲裁员送达') {
    if (!detail.type) {
      hide()
      return message.warning('地址类型不能为空，请先编辑并保存“地址类型”后再打印邮单')
    }
    if (!detail.send_datetime) {
      hide()
      return message.warning('收寄日期不能为空，请先编辑并保存“收寄日期”后再打印邮单')
    }
  }
  const [e, d] = await awaitWrap(getEmsDetailRequest(id, caseid))
  hide()
  if (e) {
    return message.error(e.message)
  }
  if (detail.status === '未送' || !detail.status) {
    updateDetailStatus(id, '打印邮单', refreshTable)
  }
  if (d) {
    common.$readerPdf(d.filename, d.id, caseid)
  } else {
    const hide = message.loading('生成中', 120)
    const [e, d] = await awaitWrap(common.$createDoc(caseid, ['EMS邮单'], detail.id, '生成EMS邮单中'))
    hide()
    if (e) {
      return message.error(e.message)
    }
    common.$readerFileById(d[0], caseid)
  }
}

// 发送二次确认
export function confirmSend (deliverid, dispatch, refreshCurrentTableFn) {
  confirm({
    icon: <ExclamationCircleOutlined />,
    content: '您确定要发送电子送达吗？',
    onOk: () => {
      sendElectronice(deliverid, dispatch, refreshCurrentTableFn)
    }
  })
}

// 发送电子送达
async function sendElectronice (deliverid, dispatch, refreshCurrentTableFn) {
  const hide = message.loading('发送中', 120)
  const [e] = await awaitWrap(sendElectronicRequest(deliverid))
  hide()
  if (e) {
    return message.error(e.message)
  }
  dispatch({ type: 'electronicDetailVisible', data: false })
  refreshCurrentTableFn()
}
